<?php

/**
 * 退货地址
 * @author renjianhong
 * @date 2021-08-08 10:47
 */
namespace Modules\Mallsetting\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Modules\Mallsetting\Http\Controllers\Controller;
use Modules\Mallsetting\Entities\RefundAddress;

class RefundAddressController extends Controller
{
    /**
     * 退货地址列表
     */
    public function list()
    { 
        return view('mallsetting::admin.refund_address.list');
    }

    /**
     * AJAX获取列表数据
     */
    public function ajaxList(Request $request ,RefundAddress $RefundAddressModel)
    {
        $pagesize = $request->input('limit'); // 每页条数
        $page = $request->input('page',1);//当前页
        $where = [
            ['is_delete', '=', '0'],
        ];
        //获取总条数
        $count = $RefundAddressModel
                ->where($where)
                ->count();
        
        //求偏移量
        $offset = ($page-1)*$pagesize;
        $list = $RefundAddressModel
                ->where($where)
                ->orderByDesc('id')->offset($offset)->limit($pagesize)->get();
        
        return $this->success(compact('list', 'count'));
    }

    /**
     * 新增编辑退货地址
     */
    public function edit(Request $request, RefundAddress $RefundAddressModel)
    {
        if($request->isMethod('post')){
            $data = $request->input();
            
            if(!$data['id']){
                $data['created_at'] = date('Y-m-d H:i:s');
                $reulst = $RefundAddressModel->insertGetId($data);
            }else{
                $reulst = $RefundAddressModel->where('id',$data['id'])->update($data);
            }
            if($reulst){
                return $this->success("操作成功");
            }else{
                return $this->failed("操作失败");
            }
        }
        $id = $request->input('id');
        $info = $RefundAddressModel->where('id',$id)->first();
        
        return view('mallsetting::admin.refund_address.edit',compact('info','RefundAddressModel'));
    }

    /**
     * 删除
     */
    public function delete(Request $request, RefundAddress $RefundAddressModel){
        if($request->isMethod('post')){
            $id = $request->input('id');
            if($id){
                $reulst = $RefundAddressModel->where('id',$id)->update(['is_delete'=>1]);
                if($reulst){
                    return $this->success('操作成功');
                }else{
                    return $this->failed('操作失败');
                }
            }else{
                return $this->failed('缺少参数id');
            }
        }
    }

    /**
     * 批量删除
     */
    public function batchDelete(Request $request, RefundAddress $RefundAddressModel){
        if($request->isMethod('post')){
            $id = $request->input('id');
            if($id){
                $reulst = $RefundAddressModel->whereIn('id',$id)->update(['is_delete'=>1]);
                if($reulst){
                    return $this->success('操作成功');
                }else{
                    return $this->failed('操作失败');
                }
            }else{
                return $this->failed('请勾选要删除的数据');
            }
            
        }
    }

}